<?php

	/**
	 * Call a function and replace/change/insert values for a specified field
	 * 
	 * 21/01/2011: function now effects type 'edit' for export 
	 * 19/12/2010: class creation
	 * 
	 * @author bsuter
	 */	
	class Devmod_method_callFunction extends Devmod_methods_abstraction {
		
		protected $after_operation = array(array(
										'for_method' => 'save',
										'for_method_type' => 'add',
										'use' => 'eval_single_row'
									), array(
										'for_method' => 'save',
										'for_method_type' => 'edit',
										'use' => 'eval_single_row'
									), array(
										'for_method' => 'save',
										'for_method_type' => 'frontend_add',
										'use' => 'eval_single_row'
									), array(
										'for_method' => 'export',
										'for_method_type' => 'list',
										'use' => 'eval_multi_row'
									), array(
										'for_method' => 'export',
										'for_method_type' => 'frontend_list',
										'use' => 'eval_multi_row'
									));
		
		/**
		 * Parameters/Arguements for "callFunction"
		 * 
		 * @param $args[0] sql_field which the function boints to
		 * @param $args[1] $args array which contains all informstions
		 * 					array(
		 * 						function => 'name_of_the_function',
		 * 						function_args => array(
		 * 							... containg all paremeters/arguments of the function which is called
		 * 						)
		 */
		public function set_config($args)
		{
			// 06.08.2010: it can be anonymus function so, function & args are not mandatory anymore	
			/* if(!$this->helper->is_valid_config(array('function', 'args'), $args[1])) {} */
			$this->update_config('call_function', array($args[0] => $args[1]));
		}
		
		/**
		 * for: type('add')->save();
		 */
		public function eval_single_row ($args) {
			
			if(!isset($this->config[$this->type]['call_function'])) return $args;
			
			$_cfg_call_function = $this->config[$this->type]['call_function'];
			
			$called_methods = array();
			
			foreach($args as $key => $value)
			{
				if(array_key_exists($key, $_cfg_call_function))
				{
					// if this function exists exectue and write back.
					if(isset($_cfg_call_function[$key]['function']))
					{
						$args[$key] = call_user_func_array($_cfg_call_function[$key]['function'], $this->helper->eval_value($value, $_cfg_call_function[$key]['args']));
					} else { // if the function does not exist creat anonymus function
						$new_function = create_function('$db', $this->helper->eval_value($value, $_cfg_call_function[$key]));
					
						$args[$key] = $new_function(ydsql::getInstance());
					}
					$called_methods[] = $key;
				}
				foreach($_cfg_call_function as $method => $options)
				{
					/*
					 * new update fix: && $key == $method
					 * but not sure this works now with all previous versions!
					 * - this fix can't work! only if the $key would be available in method!!
					 */
					if(!in_array($method, $called_methods) /* && $key == $method*/)
					{
						if(is_array($options))
						{
							$args[$method] = call_user_func_array($options['function'], $options['args']);
						} else {
							$new_function = create_function('$db', str_replace("%%value%%", $value, $options));
					
							$args[$method] = $new_function(ydsql::getInstance());
						}
					}
				}
			}
			
			return $args;
		}
		
		public function eval_multi_row($args)
		{
			if(!isset($this->config[$this->type]['call_function'])) return $args;
			
			$args = $this->helper->output_to_array($args);
			
			$_cfg_call_function = $this->config[$this->type]['call_function'];
			
			$called_methods = array();
			
			foreach($args as $row_key => $row_value)
			{
				foreach($row_value as $key => $value)
				{
					if(array_key_exists($key, $_cfg_call_function))
					{
						$args[$row_key][$key] = call_user_func_array($_cfg_call_function[$key]['function'], $this->helper->eval_value($value, $_cfg_call_function[$key]['args']));
						$called_methods[] = $key;
					}
					
					foreach($_cfg_call_function as $method => $options)
					{
						if(!in_array($method, $called_methods) && array_key_exists($key, $_cfg_call_function))
						{
							$args[$row_key][$method] = call_user_func_array($options['function'], $options['args']);
						}
					}
				}
				
			}
			
			return $args;
		}
		
	}